<?php
class installer
{
	function __construct()
	{
		if (!($this->checkpermissions()))
			die('Permissions problems.');
	}
	var $name = ""; # name of the application
	var $currentversion = ""; # self-explanatory
	var $archivetype = ""; # archive type, e.g. zip or tar.gz
	var $archivelocation = ""; # URI of archive's location
	var $filename = ""; # filename to save downloaded file to
	
	private function checkpermissions()
	{
		clearstatcache();
		$permissions = substr(decoct( fileperms('.') ), 2);
		if ($permissions != 777)
		{
			# try to chmod the directory
			chmod('.', 0777);
			clearstatcache();
			$permissions = substr(decoct( fileperms('.') ), 2);
			if ($permissions != 777)
				return false;
		}
		else
			return true;
	}
	
	function getfile($uri, $filename)
	{
		/**
		* Gets a file using file_get_contents and writes it to a specified file.  
		* 
		* @param uri		URI of file to retrieve
		* @param filename	Filename to write to
		* @return			Filename if successful, otherwise returns false
		*/

		$filestr = file_get_contents($uri);
		
		#todo: handle permissions error
		$file = fopen($filename, 'w');
		fwrite($file, $filestr);
		fclose($file);
		return $filename;
	}
	
	private function getfilesize($url)
	{

		$url = preg_replace('/http:\/\//','',$url);
		if( preg_match('/(.*?)(\/.*)/', $url, $match) )
		{
		
			$domain = $match[1];
			$portno = 80;
			$method = "HEAD";
			$url    = $match[2];
			# print "$domain\n$url\n";
			
			$http_response = "";
			$http_request .= $method." ".$url ." HTTP/1.0\r\n";
			$http_request .= "\r\n";
	
			$fp = fsockopen($domain, $portno, $errno, $errstr);
			if($fp)
			{
				fputs($fp, $http_request);
				while (!feof($fp)) $http_response .= fgets($fp, 128);
				fclose($fp);
			}
	
			$header = "Content-Length";
			$ret_str = "";
			if( preg_match("/Content\-Length: (\d+)/i",$http_response, $match ) )
			{
				return $match[1];
			}
		}
		return -1;
	}
	function percentcomplete()
	{
		$fileamount = filesize($this->filename);
		$filesize = $this->getfilesize($this->archivelocation);
		$percent = ($fileamount / $filesize) * 100;
	}
	function extract_files($extractto)
	{
		if(!file_exists($extractto))
			mkdir($extractto);
		if ($this->archivetype == 'zip')
		{
			# extract zip
			# try PECL version first
			if (ZipArchive == true)
			{
				$zip = new ZipArchive;
				if(!file_exists($this->filename))
					$file = $this->getfile($this->archivelocation, $this->filename);
				else
					$file = $this->filename;
				$openzip = $zip->open($file);
				$zip->extractTo($extractto);
					
				
			}
			else
			{
				# get our own zip extractor
				$this->getfile('http://instantinstall.org/php/Zip.txt', "Zip.php");
				require("Zip.php");
				if(!file_exists($this->filename))
					$file = $this->getfile($this->archivelocation, $this->filename);
				# extract files
				$zip = new Archive_Zip($this->filename);
				
				if ($zip->extract(array('add_path' => $extractto)))
				{
					if (!(count(scandir($extractto)) > 2))
					{
						return false;
					}
					return true;
				}
				
			}
		}
		elseif ($this->archivetype == 'tar.gz')
		{
			$this->getfile('http://instantinstall.org/php/Tar.txt', "Tar.php");
			require("Tar.php");
			if(!file_exists($this->filename))
				$file = $this->getfile($this->archivelocation, $this->filename);
			$tar = new Archive_Tar($this->filename);
			if ($tar->extract($extractto))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			echo 'Pah, unable to extract :(';
			return false;
		}
	}
}
# test code below
# $installer = new installer;
# $installer->archivetype = 'tar.gz';
# $installer->archivelocation = 'http://wordpress.org/latest.tar.gz';
# $installer->filename = 'latest.tar.gz';
# echo $installer->extract_files('tmp2');
?>